<?php
//对{lov } ... {/lov}的解析
function smarty_block_lov(&$params, $content, &$smarty, &$repeat){
   return $smarty->callblockfunc($params,$content,$smarty,$repeat, getlov);
}

/**
 * 参数：
 * 	id: 
 * 	name: 名称
 * 	page: 页码
 *  num: 数量
 * 
 * @param unknown_type $params
 */
function getlov(&$params) {
	global $_db, $cf_updatecache, $cf_pagenum;
	
	if(!isset($_SESSION['_lov_lastupdated'])) $_SESSION['_lov_lastupdated'] = 0;
	$lastupdated = &$_SESSION['_lov_lastupdated'];
	
	//#region 缓存所有字段
	if( (time()-$lastupdated>$cf_updatecache ) || !isset($_SESSION['_lovs'])){
		unset($_SESSION['_lovs']);

		$sql = "select t.* from t_lov t order by c_type asc, c_seq asc, c_created asc";
		
		//获取结果集
		$res = $_db->query($sql);
		$_SESSION['_lovs'] = array();
		foreach ($res as $row){
			//$row = array("id"=>$row['c_id'], "type"=>$row['c_type'], "name"=>$row['c_name'], "val"=>$row['c_val'], "desc"=>$row['c_desc']);
			$_SESSION['_lovs'][$row['type']][$row['name']] = $row;
			if(1==@$row['default'] || !isset($_SESSION['_lovs'][$row['type']]["__default__"]))
				$_SESSION['_lovs'][$row['type']]["__default__"] = $row;
			$_SESSION['_lovs'][0][] = $row;
		}
		
		$_SESSION['_lov_lastupdated'] = time();
		//error_log(print_r($_SESSION['_lovs']['order'],1));
	}
	//#endreg 缓存所有字段
	
	//#region 获取筛选结果集
	$res = array();
	if(array_key_exists('id', $params)){
		foreach ($_SESSION['_lovs'][0] as $field){
			if($params['id'] == $field['id']){
				$res = array($field);
				break;
			}
		}
	}
	elseif(!isempty(@$params['type']) && (!isempty(@$params['name']) || true==@$params['default'])){
		$field = null;
		if(!isempty(@$params['name']))
			$field = @$_SESSION['_lovs'][$params['type']][$params['name']];
		if(null==$field && true==@$params['default']){
			$field = @$_SESSION['_lovs'][$params['type']]["__default__"];
		}
		
		$res = array($field);
	}
	else if(!isempty(@$params['type'])){
		$field = @$_SESSION['_lovs'][$params['type']];
		//error_log("lov:".print_r($field, 1));
		if(null==$field) ;
		else{
			$res = array();
			foreach ($field as $fname => $fall){
				if("__default__"===$fname){ 
					continue;
				}
				$res[] = $fall;
			}
		}
		//error_log("lov:".print_r($res, 1));
	}
	else{
		$res = $_SESSION['_lovs'][0];
	}
	//#endreg 获取筛选结果集
	
	//if(@$params['type'] == "PAGE_NUM") error_log("lov:".print_r($res, 1));
		
	//#region 分页
	//error_log("lov:".print_r($res, 1));
	$params['totalcount'] = count($res);
	$params['pageindex'] = (!isset($params['pageindex']) || $params['pageindex']<0)?0:(int)$params['pageindex'];
	if(!array_key_exists('pagenum', $params)){
		$params['pagenum'] = $cf_pagenum;
	}
	if(0>=$params['pagenum']){
		$params['pagenum'] = $params['totalcount'];
	}
	else{
		$start = $params['pagenum']*$params['pageindex'];
		$end = $start + $params['pagenum'];
		$tmp = array();
		
		$i = 0;
		$end = $end < count($res) ? $end : count($res);
		for($i=$start; $i<$end; $i++){
			$tmp[] = $res[$i];
		}
		$res = $tmp;
	}
	//#endreg 分页
	//error_log("lov:".print_r($res, 1));
	return $res;
}
?>